﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LiveWebServiceBase;
using LiveWebServiceBase.ServiceUserSettings;

namespace LiveWebServiceAdmin
{
    public partial class Domains1 : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            string curuser = string.Empty;
            string curorg = string.Empty;
            string cursid = string.Empty;
            string curaid = string.Empty;

            if (Settings.UserInfo.IsAdminUser(this.User.Identity.Name))
            {
                if (!string.IsNullOrEmpty(Page.Request.QueryString["manageuser"]))
                {
                    curuser = Page.Request.QueryString["manageuser"];
                }
                else
                    Page.Response.Redirect("/users.aspx");
            }
            else if (Settings.UserInfo.IsServiceUser(this.User.Identity.Name))
            {
                if (!string.IsNullOrEmpty(Page.Request.QueryString["manageuser"]) && Page.Request.QueryString["manageuser"] == this.User.Identity.Name)
                {
                    curuser = Page.Request.QueryString["manageuser"];
                }
                else
                    Response.Redirect("/Login.aspx");
            }

            if (!string.IsNullOrEmpty(Page.Request.QueryString["manageorganization"]))
            {
                curorg = Page.Request.QueryString["manageorganization"];
                hdOrganization.Value = curorg;
            }

            if (!string.IsNullOrEmpty(Page.Request.QueryString["managesite"]))
            {
                cursid = Page.Request.QueryString["managesite"];
                hdSiteid.Value = cursid;
            }

            if (!string.IsNullOrEmpty(Page.Request.QueryString["manageaccounts"]))
            {
                curaid = Page.Request.QueryString["manageaccounts"];
                hdAccount.Value = curaid;
            }

            if (!string.IsNullOrEmpty(curuser) && !string.IsNullOrEmpty(curorg) && !string.IsNullOrEmpty(curaid))
            {
                hdManagedUser.Value = curuser;
                UserSettings usItem = new UserSettings(curuser);

                foreach (string domainname in usItem.GetDomains(curaid, cursid, curorg))
                {
                    lbUserDomains.Items.Add(domainname);
                }
            }
            else
                Response.Redirect("/Login.aspx");
        }

        protected void btnDeleteOrg_Click(object sender, EventArgs e)
        {
            string curuser = string.Empty;
            string curorg = string.Empty;
            string cursid = string.Empty;
            string curaid = string.Empty;

            if (Settings.UserInfo.IsAdminUser(this.User.Identity.Name))
            {
                if (!string.IsNullOrEmpty(Page.Request.QueryString["manageuser"]))
                {
                    curuser = Page.Request.QueryString["manageuser"];
                }
                else
                    Page.Response.Redirect("/users.aspx");
            }
            else if (Settings.UserInfo.IsServiceUser(this.User.Identity.Name))
            {
                if (!string.IsNullOrEmpty(Page.Request.QueryString["manageuser"]) && Page.Request.QueryString["manageuser"] == this.User.Identity.Name)
                {
                    curuser = Page.Request.QueryString["manageuser"];
                }
                else
                    Response.Redirect("/Login.aspx");
            }

            if (!string.IsNullOrEmpty(Page.Request.QueryString["manageorganization"]))
                curorg = Page.Request.QueryString["manageorganization"];

            if (!string.IsNullOrEmpty(Page.Request.QueryString["managesite"]))
                cursid = Page.Request.QueryString["managesite"];

            if (!string.IsNullOrEmpty(Page.Request.QueryString["manageaccounts"]))
                curaid = Page.Request.QueryString["manageaccounts"];

            if (lbUserDomains.SelectedItem != null)
            {
                UserSettings usItem = new UserSettings(curuser);

                usItem.DeleteDomain(curorg, cursid, curaid, lbUserDomains.SelectedItem.Text);

                Page.Response.Redirect(Page.Request.RawUrl);
            }
        }
    }
}
